package com.zsyh.jiujiu.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.utils.SPUtils;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.DemoHelper;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.HistoryLoginBean;
import com.zsyh.jiujiu.bean.MessageOrderBean;
import com.zsyh.jiujiu.bean.XitongmessageBean;
import com.zsyh.jiujiu.conference.chat.ChatActivity;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.utils.Constant;
import com.zsyh.jiujiu.utils.DbController;
import com.zsyh.jiujiu.utils.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MessageFragment extends BaseFragment {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.imagemesageorder)
    ImageView imagemesageorder;
    @BindView(R.id.tv_mes_order)
    TextView tvMesOrder;
    @BindView(R.id.remessageorder)
    RelativeLayout remessageorder;
    @BindView(R.id.imagexitong)
    ImageView imagexitong;
    @BindView(R.id.tv_mes_xitong)
    TextView tvMesXitong;
    @BindView(R.id.remessagexitong)
    RelativeLayout remessagexitong;
    @BindView(R.id.imageliaotian)
    ImageView imageliaotian;
    @BindView(R.id.tv_mes_liaotian)
    TextView tvMesLiaotian;
    @BindView(R.id.remessageliaotian)
    RelativeLayout remessageliaotian;
    @BindView(R.id.orderlist)
    RecyclerView orderlist;
    @BindView(R.id.xitonglist)
    RecyclerView xitonglist;
    @BindView(R.id.imlist)
    RecyclerView imlist;
    private DbController mDbController;
    private BaseQuickAdapter<MessageOrderBean.DataBean, BaseViewHolder> mOrderAdapter;
    List<MessageOrderBean.DataBean> Meorderlist = new ArrayList<>();

    private BaseQuickAdapter<XitongmessageBean.DataBean, BaseViewHolder> mXitongAdapter;
    List<XitongmessageBean.DataBean> Mxitonglist = new ArrayList<>();

    private BaseQuickAdapter<HistoryLoginBean, BaseViewHolder> iMAdapter;
    List<HistoryLoginBean> MiMlist ;

    @Override
    protected int initLayout() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initView(View rootView) {
        orderlist.setVisibility(View.VISIBLE);
        xitonglist.setVisibility(View.GONE);
        imlist.setVisibility(View.GONE);
        tvMesOrder.setTextColor(getResources().getColor(R.color.meaage));
        tvMesXitong.setTextColor(getResources().getColor(R.color.meaageno));
        tvMesLiaotian.setTextColor(getResources().getColor(R.color.meaageno));
        imagemesageorder.setImageResource(R.mipmap.ic_message_order);
        imagexitong.setImageResource(R.mipmap.ic_xitong);
        imageliaotian.setImageResource(R.mipmap.ic_liaotian);
        mDbController = DbController.getInstance(getActivity());

    }

    @Override
    protected void initData() {
        mOrderAdapter=new BaseQuickAdapter<MessageOrderBean.DataBean, BaseViewHolder>(R.layout.item_message_order,Meorderlist) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, MessageOrderBean.DataBean item) {

                helper.setText(R.id.title,item.getTitle());
                helper.setText(R.id.describe,item.getContent());
                helper.setText(R.id.time,item.getCreateTime());
            }
        };
        orderlist.setLayoutManager(new LinearLayoutManager(getActivity()));
        orderlist.setAdapter(mOrderAdapter);
        ordermessage((String) SPUtils.get(getActivity(), "userid", ""));


        mXitongAdapter=new BaseQuickAdapter<XitongmessageBean.DataBean, BaseViewHolder>(R.layout.item_message_order,Mxitonglist) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, XitongmessageBean.DataBean item) {
                helper.setText(R.id.title,item.getTitle());
                helper.setText(R.id.describe,item.getContent());
                helper.setText(R.id.time,item.getCreateTime());
            }
        };
        xitonglist.setLayoutManager(new LinearLayoutManager(getActivity()));
        xitonglist.setAdapter(mXitongAdapter);
        xitongmessage();

        MiMlist = mDbController.searchAll();

        iMAdapter=new BaseQuickAdapter<HistoryLoginBean, BaseViewHolder>(R.layout.item_kishiim,MiMlist) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, HistoryLoginBean item) {
                Glide.with(getActivity())
                        .load(item.getHeard())
                        .transform(new GlideCircleTransform(getActivity()))
                        .into(helper.<ImageView>getView(R.id.logo));
                helper.setText(R.id.name,item.getName());
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivity(), ChatActivity.class);
                        intent.putExtra(Constant.EXTRA_CHAT_TYPE, Constant.CHATTYPE_SINGLE);
                        intent.putExtra(Constant.EXTRA_USER_ID, item.getUserid());
                        startActivity(intent);
                    }
                });
            }
        };
        imlist.setLayoutManager(new LinearLayoutManager(getActivity()));
        imlist.setAdapter(iMAdapter);

    }

    @OnClick({R.id.remessageorder, R.id.remessagexitong, R.id.remessageliaotian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.remessageorder:
                orderlist.setVisibility(View.VISIBLE);
                xitonglist.setVisibility(View.GONE);
                imlist.setVisibility(View.GONE);
                tvMesOrder.setTextColor(getResources().getColor(R.color.meaage));
                tvMesXitong.setTextColor(getResources().getColor(R.color.meaageno));
                tvMesLiaotian.setTextColor(getResources().getColor(R.color.meaageno));
                imagemesageorder.setImageResource(R.mipmap.ic_message_order);
                imagexitong.setImageResource(R.mipmap.ic_xitong);
                imageliaotian.setImageResource(R.mipmap.ic_liaotian);
                break;
            case R.id.remessagexitong:

                orderlist.setVisibility(View.GONE);
                xitonglist.setVisibility(View.VISIBLE);
                imlist.setVisibility(View.GONE);
                tvMesOrder.setTextColor(getResources().getColor(R.color.meaageno));
                tvMesXitong.setTextColor(getResources().getColor(R.color.meaage));
                tvMesLiaotian.setTextColor(getResources().getColor(R.color.meaageno));

                imagemesageorder.setImageResource(R.mipmap.ic_message_order_no);
                imagexitong.setImageResource(R.mipmap.ic_xitong_y);
                imageliaotian.setImageResource(R.mipmap.ic_liaotian);

                break;
            case R.id.remessageliaotian:
                orderlist.setVisibility(View.GONE);
                xitonglist.setVisibility(View.GONE);
                imlist.setVisibility(View.VISIBLE);
                tvMesOrder.setTextColor(getResources().getColor(R.color.meaageno));
                tvMesXitong.setTextColor(getResources().getColor(R.color.meaageno));
                tvMesLiaotian.setTextColor(getResources().getColor(R.color.meaage));

                imagemesageorder.setImageResource(R.mipmap.ic_message_order_no);
                imagexitong.setImageResource(R.mipmap.ic_xitong);
                imageliaotian.setImageResource(R.mipmap.ic_lt);
                break;
        }
    }

    /**
     * 订单消息
     */
    public void ordermessage(String userid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getOrderMassageList(userid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("ordermessage",JsonStr+"");
                        Gson gson=new Gson();
                        MessageOrderBean messageOrderBean=gson.fromJson(JsonStr,MessageOrderBean.class);
                        if(Meorderlist!=null){
                            Meorderlist.clear();
                        }
                        for(int i=0;i<messageOrderBean.getData().size();i++){
                            Meorderlist.add(messageOrderBean.getData().get(i));
                        }
                        mOrderAdapter.notifyDataSetChanged();

                    }
                });
    }


    /**
     * 订单消息
     */
    public void xitongmessage(){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getSysMassageList()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(),false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("xitong",JsonStr+"");
                        Gson gson=new Gson();
                        XitongmessageBean messageOrderBean=gson.fromJson(JsonStr, XitongmessageBean.class);
                        if(Mxitonglist!=null){
                            Mxitonglist.clear();
                        }
                        for(int i=0;i<messageOrderBean.getData().size();i++){
                            Mxitonglist.add(messageOrderBean.getData().get(i));
                        }
                        mXitongAdapter.notifyDataSetChanged();

                    }
                });
    }

}
